library(tidyverse)
library(cowplot)
library(pdftools)
load(here::here('data/data.RData'))

data %>%
  mutate(
    SO = case_when(
      SO == "Austral: Brazilian Journal of Strategy and International Relations" ~ "Austral",
      SO == "Brazilian Journal of International Relations" ~ "BJIR",
      SO == "Conjuntura Austral: Journal of the Global South" ~ "Conjuntura Austral",
      SO == "Estudos Internacionais: Revista de Relações Internacionais da PUC Minas" ~"Estudos Internacionais",
      SO == "Meridiano 47 - Journal of Global Studies" ~ "Meridiano 47",
      SO == "Monções: Revista de Relações Internacionais da UFGD" ~ "Monções",
      SO == "Oikos - Revista de Economia Politica Internacional" ~ "Oikos",
      SO == "Revista Brasileira de Estudos de Defesa" ~ "RBED",
      SO == "Revista Brasileira de Politica Internacional" ~ "RBPI",
      TRUE ~ SO
      
    )
  ) %>% 
  filter(PY < 2022) -> data

data %>%
  group_by(SO) %>%
  count() %>%
  ungroup() %>%
  summarise(sum = sum(n))

data %>%
  filter(PY > 2015) %>%
  group_by(SO) %>%
  count() %>%
  mutate(per_year = n/6)

data %>%
  filter(PY < 2016) %>%
  group_by(SO) %>%
  count() %>%
  mutate(per_year = n/16)

data %>%
  group_by(SO) %>%
  count() %>%
  ggplot(aes(x = n, y = reorder(SO,-n))) +
  geom_col(fill = "#444444") +
  geom_text(aes(label = n),
            hjust = 1.4,
            col = "white",
            fontface = "bold",
            size = 3) +
  labs(
    x = "ARTICLES",
    y = ""
  ) +
  theme_minimal() +
  scale_x_continuous(limits = c(0,1200), breaks = c(0,1200)) +
  theme(
    axis.text = element_text(color = '#444444'),
    plot.margin = margin(10,10,10,10),
    panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(),
    axis.title.x = element_text(
      margin = margin(t = 10)
    ),
    axis.title.y = element_text(
      margin = margin(r = 10)
    )
  ) -> plot2


data %>%
  group_by(SO, PY) %>%
  count() %>%
  ungroup() %>%
  group_by(SO) %>%
  count() %>%
  rename("n_years" = n) -> n_years

data %>%
  group_by(SO) %>%
  count() %>%
  rename("n_articles" = n) %>%
  left_join(.,n_years) %>%
  mutate(
    articles_per_year = n_articles/n_years
  ) %>%
  select(SO, articles_per_year) %>%
  ggplot(aes(x = articles_per_year, y = reorder(SO,-articles_per_year))) +
  geom_col(fill = "#444444") +
  geom_text(
    aes(label = round(articles_per_year,2)),
    hjust = 1.4,
    col = "white",
    fontface = "bold",
    size = 3
  ) +
  labs(
    x = "ARTICLES / YEAR",
    y = ""
  ) +
  theme_minimal() +
  theme(
    axis.text = element_text(color = '#444444'),
    plot.margin = margin(10,10,10,10),
    panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(),
    axis.title.x = element_text(
      margin = margin(t = 10)
    ),
    axis.title.y = element_text(
      margin = margin(r = 10)
    )
  ) -> plot3

plot_grid(plot2, plot3, labels = c("A","B"))


ggsave(
  plot = plot_grid(plot2, plot3, labels = c("A","B")),
  filename = here::here('outputs/figure-02.pdf'),
  device = cairo_pdf,
  dpi = 400
)

pdf_convert(pdf = here::here('outputs/figure-02.pdf'),filenames = "outputs/figure-02.jpeg",
            format = "jpeg",dpi = 400)
